package com.scy.edu.service.impl;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.scy.edu.ZooktaskApplication;
import com.scy.edu.entity.Account;
import com.scy.edu.service.AccountService;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 司晨雨
 * @date 2020/10/11 20:59
 * @description
 */
@Service
public class AccountServiceImpl implements AccountService {

    public List<Account> getAllList() {
        List<Account> accountList = new ArrayList<Account>();

        ResultSet rs = null;
        DruidPooledConnection conn =  null;
        PreparedStatement ps = null;
        try {
            conn = ZooktaskApplication.dataSource.getConnection();
            String sql = "SELECT * from account";
            ps=conn.prepareStatement(sql);
            rs = ps.executeQuery();
            if(rs.next()){
                Account account = new Account();
                account.setName(rs.getString("name"));
                account.setCardNo(rs.getString("money"));
                account.setMoney(rs.getString("cardNo"));
                accountList.add(account);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (conn != null) {
                    conn.close();
                }
                if (ps != null) {
                    ps.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return accountList;
    }
}
